
#include "MCU.h"

#include "BSP.h"

#include "Buffer.h"

#include "ID_Process.h"


#define ID_BAUD		115200
#define ID_UART		1

static Buffer_T* ID_UartRxBuffer = 0;

//	void (*bspUartCallback)(uint8_t rx);

#if 0
void USART1_IRQHandler(void)
{
    if(RESET != usart_interrupt_flag_get(ID_UART, USART_INT_FLAG_RBNE)){
        /* read one byte from the receive data register */
        uint8_t rx = (uint8_t)usart_data_receive(ID_UART);
		if(ID_UartRxBuffer){
			Buffer_T* buf = ID_UartRxBuffer;			
			buf->buffer[buf->offset++] = rx;
			if(buf->offset == 7){
				buf->length = buf->buffer[5] * 256 + buf->buffer[6] + 5 + 2;
			}
			else if(buf->offset > 7){
				if(buf->length == buf->offset){
					ID_UartRxBuffer = 0;
					ID_Uart_Notify();
				}
			}
		}
    }
}

void ID_Uart_Init(void)
{
	uint32_t com = ID_UART;
	
    /* USART interrupt configuration */
    nvic_irq_enable(USART1_IRQn, 2, 3);

	/* enable GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOA);

    /* enable USART clock */
    rcu_periph_clock_enable(RCU_USART1);

    /* connect port to USARTx_Tx */
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);

    /* connect port to USARTx_Rx */
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_3);

    /* USART configure */
    usart_deinit(com);
    usart_baudrate_set(com, 115200U);
    usart_word_length_set(com, USART_WL_8BIT);
    usart_stop_bit_set(com, USART_STB_1BIT);
    usart_parity_config(com, USART_PM_NONE);
    usart_hardware_flow_rts_config(com, USART_RTS_DISABLE);
    usart_hardware_flow_cts_config(com, USART_CTS_DISABLE);
    usart_receive_config(com, USART_RECEIVE_ENABLE);
    usart_transmit_config(com, USART_TRANSMIT_ENABLE);
    usart_enable(com);	

	/* enable  receive interrupt */
    usart_interrupt_enable(com, USART_INT_RBNE);
}
#endif 
void ID_Uart_Rx(uint8_t rx)
{
	if(ID_UartRxBuffer){
		Buffer_T* buf = ID_UartRxBuffer;
		buf->buffer[buf->offset++] = rx;
		if(buf->offset == 7){
			buf->length = buf->buffer[5] * 256 + buf->buffer[6] + 5 + 2;
		}
		else if(buf->offset > 7){
			if(buf->length == buf->offset){
				ID_UartRxBuffer = 0;
				ID_Uart_Notify();
			}
		}
	}
}

void ID_Uart_Init(void)
{
	BSP_Uart_SetRxCallback(ID_UART, ID_Uart_Rx);
	BSP_Uart_Init(ID_UART);
}


void ID_Uart_SetBaud(uint32_t baud){
//		usart_baudrate_set(ID_UART, baud);
	BSP_Uart_SetBaud(ID_UART, baud);
}

void ID_Uart_SendByte(uint8_t tx){
	BSP_Uart_Send(ID_UART, tx);
#if 0
	volatile uint32_t i = 0;
	
    while(RESET == usart_flag_get(ID_UART, USART_FLAG_TBE)){
		if(++i > 100000){
			break;
		}
	}
    usart_data_transmit(ID_UART, tx);
#endif 
}


void ID_Uart_Send(uint8_t buf[], uint32_t len){
	uint32_t i;
	
	for(i = 0; i < len; i++){
		ID_Uart_SendByte(buf[i]);
	}
}

void ID_Uart_SendBuffer(Buffer_T* buf){
	ID_Uart_Send(buf->buffer, buf->length);
}


void ID_Uart_SetupReceive(Buffer_T* rxBuffer){
	rxBuffer->length = 0;
	rxBuffer->offset = 0;
	ID_UartRxBuffer = rxBuffer;
}

uint32_t ID_Uart_CheckRxCompelet(void){
	return ID_UartRxBuffer == 0;
}


